package com.wnxy.sddfp.bank.service.impl;
import java.util.Date;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnxy.sddfp.bank.entity.TradeLog;
import com.wnxy.sddfp.bank.mapper.TradeLogMapper;
import com.wnxy.sddfp.bank.service.ITradeLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDto;
import com.wnxy.sddfp.common.model.vo.trade.BorrowSyncRecordVo;
import com.wnxy.sddfp.common.util.BaseResult;
import com.wnxy.sddfp.common.util.EncryptUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 托管系统交易记录表 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2024-01-17
 */
@Service
public class TradeLogServiceImpl extends ServiceImpl<TradeLogMapper, TradeLog> implements ITradeLogService {

    @Autowired
    private RestTemplate restTemplate;

    @SneakyThrows
    @Override
    public SignatureDto proxyBindCardLog(BankCardAddDTO bankCardAddDTO) {

        // 保存交易记录
        TradeLog tradeLog = new TradeLog();
        tradeLog.setReqCode(bankCardAddDTO.getReqCode());
        tradeLog.setReqType("USER_BIND_CARD");
        tradeLog.setObjectId(bankCardAddDTO.getUserId());
        tradeLog.setCreateDate(new Date());
        tradeLog.setSynchronizeStatus(0);
        tradeLog.setConfirmDate(new Date());
        this.save(tradeLog);

        // 返回结果
        SignatureDto signatureDto = new SignatureDto();
        signatureDto.setBankUrl("http://localhost:5600/bank/user");
        signatureDto.setPlatformNo("SDDFP");
        signatureDto.setReqData(new ObjectMapper().writeValueAsString(bankCardAddDTO));
        signatureDto.setReqCode(bankCardAddDTO.getReqCode());
        return signatureDto;
    }

    @Override
    @SneakyThrows
    public void sysncBorrowRecord(BorrowSyncRecordVo borrowSyncRecordVo) {
        // 保存交易日志
        TradeLog tradeLog = new TradeLog();
        tradeLog.setReqCode(borrowSyncRecordVo.getReqCode());
        tradeLog.setReqType("CREATE");
        tradeLog.setObjectId(borrowSyncRecordVo.getId());
        tradeLog.setCreateDate(new Date());
        tradeLog.setSynchronizeStatus(0);
        this.save(tradeLog);

        // 远程调用银行托管系统
        String url = "http://localhost:5600/service";
        String str = new ObjectMapper().writeValueAsString(borrowSyncRecordVo);
        String base64 = EncryptUtil.encodeUTF8StringBase64(str);
        String reqData = EncryptUtil.encodeURL(base64);
        // 远程调用
        this.sendHttpGet(url,"SYNC_BORROW_RECORD",reqData,tradeLog);
    }

    /**
     * 通过RestTemplate远程调用银行托管系统，修改交易日志状态为1已同步
     * @param url 银行托管系统请求基础路径
     * @param serviceName 区分托管系统不同方法的
     * @param reqData 请求数据的json格式
     * @param tradeLog 日志对象
     */
    @SneakyThrows
    private void sendHttpGet(String url,String serviceName,String reqData,TradeLog tradeLog) {
        StringBuffer sb = new StringBuffer();
        sb.append(url);
        sb.append("?serviceName=").append(serviceName);
        sb.append("&reqData=").append(reqData);

        // 通过restTemplate远程调用银行托管系统
        String responseBody = restTemplate.getForObject(sb.toString(), String.class);
        //BaseResult baseResult = new ObjectMapper().readValue(responseBody, BaseResult.class);

        // 修改交易日志的同步状态、数据同步时间
        tradeLog.setSynchronizeStatus(1);
        tradeLog.setConfirmDate(new Date());
        this.updateById(tradeLog);
    }
}
